from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional
from decimal import Decimal

class CourseBase(BaseModel):
    course_code: str = Field(..., description="课程编码", max_length=20)
    course_name: str = Field(..., description="课程名称", max_length=100)
    course_type: int = Field(..., description="课程类型：1-主科 2-副科 3-活动课")
    grade_range: Optional[str] = Field(None, description="适用年级范围（如：1-2,3-5）", max_length=20)
    credit: Optional[Decimal] = Field(None, description="学分", decimal_places=1)
    course_hours: Optional[int] = Field(None, description="课时数")
    course_desc: Optional[str] = Field(None, description="课程描述", max_length=500)
    status: int = Field(1, description="状态：0-停用 1-启用")

class CourseCreate(CourseBase):
    """创建课程时使用的模型"""
    pass

class CourseUpdate(BaseModel):
    """更新课程时使用的模型"""
    course_code: Optional[str] = None
    course_name: Optional[str] = None
    course_type: Optional[int] = None
    grade_range: Optional[str] = None
    credit: Optional[Decimal] = None
    course_hours: Optional[int] = None
    course_desc: Optional[str] = None
    status: Optional[int] = None

class Course(CourseBase):
    id: int
    create_time: Optional[datetime] = Field(None, description="创建时间")
    update_time: Optional[datetime] = Field(None, description="更新时间")
    
    class Config:
        from_attributes = True